package feizhou.untils.tools;

import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则工具
 *
 * @author <a href="920786312@qq.com">周飞</a>
 * @since 2023/7/27 16:07
 */
public class RegexUtil {

    public static Logger log = Logger.getLogger(String.valueOf(RegexUtil.class));

    public static final String CHINA_AREA_CODE = "86";

    //匹配以下格式的图片
    //![](./image2/14.png)
    //![在这里插入图片描述](https://img-blog.csdnimg.cn/0f2cda836c474df698f6042e1fc42253.png#pic_center)
    public static final String image_regex = "(\\./image\\d+/\\d+(\\.jpeg|\\.png|\\.jpg||\\.gif||\\.bmp))|(https://img-blog.csdnimg.cn/.*(\\.jpeg|\\.png|\\.jpg|\\.gif|\\.bmp))";

    /**
     * 是否手机号
     *
     * @param areaCode
     * @param mobiles
     * @return boolean
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/7/27 16:08
     */
    public static boolean isMobileNO(String areaCode, String mobiles) {
        if (mobiles == null) {
            return false;
        }
        if (StringUtil.isEmpty(areaCode) || CHINA_AREA_CODE.equals(areaCode)) {
            Pattern p = Pattern.compile("^1\\d{10}$");
            Matcher m = p.matcher(mobiles);
            return m.matches();
        } else {
            Pattern p = Pattern.compile("^\\d{1,20}$");
            Matcher m = p.matcher(mobiles);
            return m.matches();
        }
    }


    /**
     * 是否数字
     *
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/7/27 16:09
     */
    public static boolean isNumber(String str) {
        if (str == null) {
            return false;
        }
        Pattern p = Pattern.compile("^\\d+$");
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 字符串匹配
     *
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/7/27 16:09
     */
    public static boolean stringMatcher(String context, String regex) {
        if (context == null) {
            return false;
        }
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(context);
        return m.find();
    }

    /**
     * 提取内容
     *
     * @param context
     * @param regex
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/10/8 15:24
     */
    public static String findText(String context, String regex) {
        if (context == null) {
            return null;
        }
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(context);
        if (m.find()) {
            return m.group();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
//        String context = "![s入图s](./image2/14.png)";
//        String context = "![](./image2/14.png)";

        String context = "![在这里插入图片描述](https://img-blog.csdnimg.cn/0f2cda836c474df698f6042e1fc42253.png#pic_center)";
//        String context = "123";
//        String regex = "!\\[\\S*\\]\\((\\./image\\d+/\\d+(\\.jpeg|\\.png|\\.jpg||\\.gif||\\.bmp))|(https://img-blog.csdnimg.cn/.*)";
        String regex = "(\\./image\\d+/\\d+(\\.jpeg|\\.png|\\.jpg||\\.gif||\\.bmp))|(https://img-blog.csdnimg.cn/.*(\\.jpeg|\\.png|\\.jpg|\\.gif|\\.bmp))";
//        String regex = "(\\./image\\d+/\\d+(\\.jpeg|\\.png|\\.jpg||\\.gif||\\.bmp))|(https://img-blog.csdnimg.cn/.*(\\.png|\\.jpg|\\.jpeg|\\.gif))";
//        String context = "temp222.md)";
//        String regex = "temp.*\\.md";
        System.out.println(stringMatcher(context, regex));
        System.out.println(findText(context, regex));


    }
}
